package likou;

/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/03/13 08:46
 */
public class _331 {
    public static void main(String[] args) {
        System.out.println(isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#"));
    }
    public static boolean isValidSerialization(String preorder) {
        int slot=1;
        if(preorder==null) return false;
        char[] s = preorder.toCharArray();
        int n = s.length;
        if(n==1&&s[0]=='#') return true;
        for(int i=1;i<n;i++){
            if(s[i]==',') {
                if(s[i-1]!='#'){
                    if(slot<=0) return false;
                    slot++;
                } else {
                    slot--;
                }
            }
            if(i==n-1){
                if(s[i]=='#') slot--;
                else slot++;
            }
        }
        if(slot==0) return true;
        else return false;
    }
}
